﻿using JinJun.EF.Permission.DAL;
using JinJun.EF.Permission.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace JinJun.EF.Permission.BLL
{
    public class BaseService<T> where T:class
    {
        //当前操作数据库的实例
        private BaseRepository<T, PermissionEntities> baseRepository = new BaseRepository<T, PermissionEntities>();


        /// <summary>
        /// 单表的添加一条记录
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public virtual bool Add(T entity)
        {
            //调用单表的添加一条记录方法
            return baseRepository.Add(entity);          
        }


        /// <summary>
        /// 单表的添加多条记录
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public virtual bool AddRange(List<T> entities)
        {
            //调用单表的添加多条记录
            return baseRepository.AddRange(entities);
        }



        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public virtual bool Delete(T entity)
        {
            //调用删除
            return baseRepository.Delete(entity);
        }


        /// <summary>
        ///删除多个实体
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public virtual bool BatchDelete(List<T> entities)
        {
            //调用删除多个实体
            return baseRepository.BatchDelete(entities);
        }



        /// <summary>
        ///删除多个实体根据Id
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public virtual bool BatchDeleteById(params int[] ids)
        {
            //调用删除多个实体根据Id
            return baseRepository.BatchDeleteById(ids);
        }



        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public virtual bool Update(T entity)
        {
            //调用修改
            return baseRepository.Update(entity);
        }



        /// <summary>
        /// 查询返回一个集合
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public virtual List<T> QueryList(Expression<Func<T, bool>> lambdaExpression)
        {
            //调用修改
            return baseRepository.QueryList(lambdaExpression);
        }


        /// <summary>
        /// 查询返回一个对象,没有返回null---根据主键查
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public virtual T Query(Expression<Func<T, bool>> lambdaExpression)
        {
            //调用查询返回一个对象,没有返回null---根据主键查
            return baseRepository.Query(lambdaExpression);
        }


        /// <summary>
        ///判断是否存在
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public virtual bool Exists(Expression<Func<T, bool>> lambdaExpression)
        {
            //调用判断是否存在
            return baseRepository.Exists(lambdaExpression);
        }



        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public virtual List<T> QueryPageList<S>(int pageIndex, int pageSize, Expression<Func<T, bool>> whereLambda, Expression<Func<T, S>> orderbyLambda, out int count, bool isAsc = true)
        {
            //调用分页查询
            return baseRepository.QueryPageList(pageIndex, pageSize, whereLambda, orderbyLambda,out count, isAsc);
        }
    }
}
